﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Profile;
using System.Data;

namespace WebSite.cn
{
    public partial class messages : System.Web.UI.Page
    {
        // Fields

        public string msg = "";

        // Methods
        private void Bind()
        {
            this.ddlMsgType.Items.Clear();
            string sql = "select * from tbCompain";
            DataTable table = SQLAccess.GreatDs(sql).Tables[0];
            if (table.Rows.Count > 0)
            {
                foreach (DataRow row in table.Rows)
                {
                    this.ddlMsgType.Items.Add(new ListItem(row["CompainName"].ToString(), row["CompainId"].ToString()));
                }
                this.ddlMsgType.Items[0].Selected = true;
            }
            if (this.Session["memberid"] != null)
            {
                sql = "select * from DB_Msg where memberid=" + this.Session["memberid"] + " order by updatetime desc";
            }
            else
            {
                sql = "select * from DB_Msg order by updatetime desc";
            }
            table = SQLAccess.GreatDs(sql).Tables[0];
            if (table.Rows.Count > 0)
            {
                foreach (DataRow row2 in table.Rows)
                {
                    this.msg = this.msg + "<tr>";
                    this.msg = this.msg + "<td width='91%' height='28' align='left' bgcolor='#FFF8E8'>";
                    if (this.Session["memberid"] != null)
                    {
                        this.msg = this.msg + "<span class='del'><a href='messages.aspx?action=del&msgid=" + row2["msgid"].ToString() + "'><img src='images/msg_delete.gif' />删除</a></span>";
                    }
                    DataTable table2 = SQLAccess.GreatDs("select msgtype from DB_Msgtype where msgtypeid=" + row2["msgtypeid"].ToString() + "").Tables[0];
                    if (table2.Rows.Count > 0)
                    {
                        this.msg = this.msg + "<span class='yell'><b>" + table2.Rows[0][0].ToString() + ":</b>";
                    }
                    string msg = this.msg;
                    this.msg = msg + "</span>" + row2["title"].ToString() + "&nbsp; " + row2["updatetime"].ToString() + "<div>" + row2["content"].ToString() + "</div></td>";
                    this.msg = this.msg + "</tr>";
                    this.msg = this.msg + "<tr>";
                    this.msg = this.msg + "<td height='28' align='left'><strong>回复</strong>：" + row2["replay"].ToString() + "</td>";
                    this.msg = this.msg + "</tr>";
                }
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string str = "0";
            if (this.Session["memberid"] != null)
            {
                str = this.Session["memberid"].ToString();
            }
            SQLAccess.DoSql(string.Concat(new object[] { "insert into DB_Msg (msgtypeid,memberid,title,content,updatetime,telphone) values (", this.ddlMsgType.SelectedItem.Value, ",", str, ",'", re.replace(base.Request.Form["title"]), "','", re.replace(base.Request.Form["content"]), "','", DateTime.Now, "','", re.replace(this.telphone.Value), "')" }));
            base.Response.Write("<script language='javascript'>alert('提交成功!');window.location.href='messages.aspx';</script>");
            base.Response.End();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                this.Bind();
                if (this.Session["memberid"] != null)
                {
                    DataSet dsMsg = SQLAccess.GreatDs("select tel1 from DB_Member where memberid = " + this.Session["memberid"].ToString());
                    if (dsMsg != null && dsMsg.Tables.Count > 0 && dsMsg.Tables[0].Rows.Count > 0)
                    {
                        this.telphone.Value = dsMsg.Tables[0].Rows[0][0].ToString();
                    }
                }
            }
        }

 

    }
}